﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExamCommon;

namespace ExamClientBLL
{
    public class LoginService
    {
        // 委托集合, 里面的委托用于控制UI
        private static List<Action<string>> actionList = new List<Action<string>>();

        /// <summary>
        /// 初始化委托集合
        /// </summary>
        /// <param name="actions"></param>
        public static void initAction(List<Action<string>> actions)
        {
            actionList.AddRange(actions);
        }

        /// <summary>
        /// 用户请求登录
        /// </summary>
        /// <param name="loginDTO"></param>
        /// <returns></returns>
        public static bool LoginRequset(LoginDTO loginDTO)
        {
            bool flag = ClientSocketService.CreateClientTCPSocket(loginDTO.ipAddress, loginDTO.port);
            if (flag == false)
            {
                return false;
            }
            Message loginMsg = new Message
            {
                msgCommand = MsgCommand.LOGIN,
                objectData = loginDTO
            };
            return ClientSocketService.SendMessage(loginMsg);
        }

        /// <summary>
        /// 用户登录响应
        /// </summary>
        /// <param name="msg"></param>
        public static void LoginResponse(Message msg)
        {
            switch (msg.msgCommand)
            {
                case MsgCommand.LOGIN_SUCCESS:
                    Console.WriteLine("====登录成功====");
                    actionList[1](string.Empty);
                    break;
                case MsgCommand.REPEAT_LOGIN:
                    Console.WriteLine("====登录失败！原因： 重复登录====");
                    actionList[0]("重复登录! 检测到您已经在服务器上登录了，无需重复登录。如有疑问请联系监考人员!");
                    break;
                case MsgCommand.LOGIN_FAIL:
                    Console.WriteLine("====登录失败====");
                    actionList[0]("登录失败！请检查信息后再试");
                    break;
                default:
                    Console.WriteLine("info: ===================未处理的登录响应===================");
                    return;
            }
        }


    }
}
